package com.burgeon.backend.SHOP.rest;
/**
 * 创建退款单
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Refund;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CreateRefund extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateRefund(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }
    /**
     * 发送并接收字符
     */
    public String getData(Refund refund) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(refund), url);
        System.err.println(str);
        JSONObject jsonObject = new JSONArray(str).getJSONObject(0);
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");

         if (!"0".equals(code)) {
            throw new Exception(message);
        }

        return jsonObject.getString("objectid");
    }

    private MultiValueMap Data(Refund refund) throws Exception{

        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "YC_RET_RETAIL");
        masterobj.put("ID", "-1");
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(now);
        masterobj.put("BILLDATE", dateString);
        masterobj.put("XSNO", refund.getOrderId());//线上单号
        masterobj.put("XSRETNO",refund.getRefundId());//线上退款单号
        masterobj.put("RET_DESCRIPTION", refund.getCause());//退货原因

        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        FIndAD_Table fIndAD_table = new FIndAD_Table(ip,account,password);
        HashMap yc_retail = fIndAD_table.getData("YC_RET_RETAIL");
        list.add(yc_retail.get("YC_RET_RETAILITEM"));

        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "YC_RET_RETAILITEM");//
        List addList1 = new ArrayList();
        HashMap map1 = new HashMap();//addlist内容
        map1.put("id", "-1");

        map1.put("M_PRODUCT_ID__NAME", refund.getCode());
        map1.put("PRICEACTUAL", refund.getPrice());
        map1.put("QTY", refund.getNum());
        map1.put("TOT_AMT_ACTUAL", refund.getTotalPrice());

        addList1.add(map1);

        ref1.put("addList", String.valueOf(addList1));

        refobjs.add(ref1);

        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        params.put("submit", "true");//是否提交
        return DATA("ProcessOrder", params, account, password);
    }

    public static void main(String[] args) {
        CreateRefund g = new CreateRefund("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        Refund refund = new Refund();
        refund.setCode("BDN2LD155K401");
        refund.setOrderId("123124122");
        refund.setCause("原因");
        refund.setPrice("1");
        refund.setNum("2");
        refund.setTotalPrice("2");
        try {
            g.getData(refund);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
